package MapReduce.Demo8_TemperatureAvgByMonth;

import MapReduce.writableBean.TemperatureWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @Author lixinlei
 * @Date 2023/3/21 9:27
 */
public class TemperatureAvgByMonthCombiner extends Reducer<Text, TemperatureWritable,Text,TemperatureWritable> {

    TemperatureWritable outValue = new TemperatureWritable();

    @Override
    protected void reduce(Text key, Iterable<TemperatureWritable> values, Reducer<Text, TemperatureWritable, Text, TemperatureWritable>.Context context) throws IOException, InterruptedException {
        int tempSum = 0;
        int tempCount = 0;
        for (TemperatureWritable value : values) {
            tempSum += value.getTemperature();
            tempCount += value.getCount();
        }

        outValue.setTemperature(tempSum);
        outValue.setCount(tempCount);

        context.write(key,outValue);
    }
}
